package tool

import java.io.File
import cats.data.Validated
import implicits.Implicits._
import play.api.i18n.Messages
import tool.Pojo.{MyMessage, SampleInfoFileCheckData}

import scala.collection.mutable

/**
 * Created by yz on 12/3/2020
 */
object FileTool {

  def fileCheck(file: File)(implicit messages: Messages) = {
   FileTool.dataFileCheck(file)
  }

  def dataFileCheck(file: File)(implicit messages: Messages) = {
    MetDataFileValidTool.valid(file.txtLines)
  }

  def groupFileCheck(file: File)(implicit messages: Messages) = {
    GroupFileValidTool.valid(file.txtLines)
  }

  def groupFileCheckHeader(file: File)(implicit messages: Messages) = {
    GroupFileValidTool.validHeaders(file.txtLines)
  }

  def listFileCheck(file: File)(implicit messages: Messages) = {
    VarInfoFileValidTool.valid(file.txtLines)
  }

  def libFileCheck(file: File)(implicit messages: Messages) = {
    LibFileValidTool.valid(file.txtLines)
  }

  def libFileCheckHeader(file: File)(implicit messages: Messages) = {
    LibFileValidTool.validHeaders(file.txtLines)
  }

  def metaboFileCheck(file: File): MyMessage = {
    val fileMessage = "数据文件"
    val lines = file.xlsxLines()
    val headers = lines.head
    val repeatHeaders = headers.diff(headers.distinct)
    if (repeatHeaders.nonEmpty) {
      return MyMessage(false, s"数据文件表头 ${repeatHeaders.head} 重复!")
    }
    val hasHeaders = List("Index", "Name", "Standard.name", "Class", "HMDBID", "KeggID")
    val noExistHeaders = hasHeaders.diff(headers)
    if (noExistHeaders.nonEmpty) {
      return MyMessage(false, s"数据文件表头 ${noExistHeaders.head} 不存在!")
    }
    val repeatColumns = List("Index")
    val repeatMap = repeatColumns.map(x => (x, mutable.Set[String]())).toMap
    lines.drop(1).zipWithIndex.foreach { case (tmpColumns, i) =>
      val columns = tmpColumns.padTo(headers.size, "")
      if (columns.size > headers.size) {
        return MyMessage(false, s"${fileMessage}第${i + 2}行列数不正确,存在多余列!")
      }
      columns.zipWithIndex.foreach { case (column, j) =>
        val header = headers(j).toLowerCase
        if (repeatColumns.contains(header)) {
          if (repeatMap(header).contains(column)) {
            return MyMessage(false, s"${fileMessage}第${i + 2}行第${j + 1}列重复!")
          } else repeatMap(header) += column
        }

      }
    }

    MyMessage(true, "")

  }


}
